package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 功能描述: 类型分页
     *
     * @param categoryPageQueryDTO
     * @return : com.sky.result.PageResult
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page pageInfo = categoryMapper.page(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        return new PageResult(pageInfo.getTotal(), pageInfo.getResult());
    }

    /**
     * 功能描述: 新增类型
     * 1 菜品类型 2套餐类型
     * @return : boolean
     */
    @Override
    public boolean save(CategoryDTO categoryDTO) {
        Long id = BaseContext.getCurrentId();
        Category category = new Category();
        //拷贝数据
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        return  categoryMapper.save(category);
    }

    /**
     * 功能描述: 类型启用禁用
     * @return : boolean
     */
    @Override
    public boolean enableOrDisable(int status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        return categoryMapper.update(category);
    }

    /**
     * 功能描述: 类型修改
     * @return : boolean
     */
    @Override
    public boolean update(CategoryDTO categoryDTO) {
        //将数据拷贝到category
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        return categoryMapper.update(category);
    }

    /**
     * 功能描述: 根据id删除
     * @return : boolean
     */
    @Override
    public boolean delete(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Dish dish = dishMapper.findById(id);
        if(dish!=null){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        Integer count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        return categoryMapper.delete(id);
    }
    /**
     * 功能描述: 根据类型查询分类
     * @return : List<Category>
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

}
